<?php

/*
 * This class is used for as Model for Product table 
 */

class Model_Propertysubtagtype extends Model_Base {

    protected $_name = 'prp_sub_tag';
    protected $_primaryKey = 'prp_sub_tag_id';

    
    
    public function  fetchsubcategoryArray($where = ""){
        $finalArray = array();
        $result = $this->fetchallsubtag($where,"","0",'prp_sub_tag_name','ASC');
        $finalArray[''] = 'Select Subcategory';
        if($where!= ''){
            foreach ($result as $user) {
                $finalArray[$user['prp_sub_tag_id']] = $user['prp_sub_tag_name'];
            }
        }
        return $finalArray;
    }
    
    
    public function fetchEntryByREFId($id) {
        $select = $this->select()
                ->from(array('prtyp' => 'prp_sub_tag'))
                ->setIntegrityCheck(false)
                ->join(array('ln' => 'languages'), "prtyp.prp_sub_tag_lang_id = ln.lang_id")
                ->where('prtyp.prp_sub_tag_publish != 2')
                ->where('prp_subtag_ref_id = ?', $id);
        // see reasoning in fetchEntries() as to why we return only an array
        $data = $this->fetchAll($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }
    
    public function fetchEntryById($id) {
        $select = $this->select()
                ->from(array('prtyp' => 'prp_sub_tag'))
                ->setIntegrityCheck(false)
                ->join(array('ln' => 'languages'), "prtyp.prp_sub_tag_lang_id = ln.lang_id")
                ->where('prtyp.prp_sub_tag_publish != 2')
                ->where('prp_sub_tag_id = ?', $id);
        // see reasoning in fetchEntries() as to why we return only an array
        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }
    
    
    /**
     *  This function is used to get Questions
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchPropertysubtagtype($where = "", $limit = "", $offset = "0", $order = 'prp_sub_tag_id', $sort = 'ASC') {
        $select = $this->select()
                ->from(array('prtyp' => 'prp_sub_tag'));
        if ($where != '') {
            $select->where($where);
        }
        if ($limit != "") {
            $select->limit($limit, $offset);
        }
        $select->order($order . " " . $sort);
        $data = parent::fetchAll($select);
        if (!empty($data)) {
            $finalArray = array();
            foreach($data->toArray() as $_property_type) {
                $finalArray[$_property_type['prp_sub_tag_id']] = $_property_type['prp_tgtype_title'];
            }
            return $finalArray;
        } else {
            return array();
        }
    }
    
     public function fetchallsubtag($where = "", $limit = "", $offset = "0", $order = 'prp_sub_tag_id', $sort = 'ASC', $group = 'prp_subtag_ref_id') {
       $select = $this->select()
                ->from(array('prtyp' => 'prp_sub_tag'), array('*','lang_names' => new Zend_Db_Expr('GROUP_CONCAT(ln.lang_name ORDER BY ln.lang_id)')) )
                ->setIntegrityCheck(false)
                ->join(array('cat' => 'property_tag_type_name'), "cat.prp_typename_id = prtyp.prp_main_tag_id")
                ->join(array('ln' => 'languages'), "prtyp.prp_sub_tag_lang_id = ln.lang_id")
                ->where('prtyp.prp_sub_tag_publish != 2')
                ->where('prp_sub_tag_name != ""' );
         if ($where != '') {
            $select->where($where);
        }
        if ($group != "") {
            $select->group($group);
        }
       //  $order = " `ln`.`lang_id`, `prtyp`.`prp_sub_tag_id`";
        $select->order($order . " " . $sort);
        $result = $this->fetchAll($select);
          #  echo $select;exit;;
        if (!empty($result)) {
            return $result->toArray();
        } else {
            return array();
        }
    }
    
   
    /**
     *  This function is used to get total orders
     * @return int
     */
    public function getSubcategoryCount($where) {
        
        $select = $this->select()
                ->from(array('prtyp' => 'prp_sub_tag'), Array('cnt' => 'count(*)'))
                ->setIntegrityCheck(false)
                ->join(array('cat' => 'property_tag_type_name'), "cat.prp_typename_id = prtyp.prp_main_tag_id")
                ->join(array('ln' => 'languages'), "prtyp.prp_sub_tag_lang_id = ln.lang_id")
                ->where('prtyp.prp_sub_tag_publish != 2')
                ->where('prp_sub_tag_name != ""' )
                ->group('prp_subtag_ref_id');
        if ($where != '') {
            $select->where($where);
        }

        $rows = parent::fetchRow($select);
        return($rows->cnt);
    }
    
    
}
